import {formatDate} from "../../../../hrpub/common/utils/utils";
import {getSignStatus} from "./commonFun";
import getSysConf from "../../../public/functions/getSysConf";

export default class ListAction {
    constructor(comp) {
        this.comp = comp;
    }

    onBtnOperation = () => {

    }

    handlePageIndexChange = (pageIndex) => {
        const {dispatch, myLeave} = this.comp.props
        let pageInfo = myLeave.activeTab + 'PageInfo'
        dispatch({
            type: 'myLeave/update',
            payload: {
                [pageInfo]: Object.assign({}, myLeave[pageInfo], {
                    pageIndex
                })
            }
        })
        this.queryListAction()
    }

    handlePageSizeChange = (pageSize) => {
        const {dispatch, myLeave} = this.comp.props
        let pageInfo = myLeave.activeTab + 'PageInfo'
        dispatch({
            type: 'myLeave/update',
            payload: {
                [pageInfo]: Object.assign({}, myLeave[pageInfo], {
                    pageSize
                })
            }
        })
        this.queryListAction()
    }

    queryListAction = () => {
        const {dispatch, myLeave, editTable} = this.comp.props;
        const {beginValue, endValue} = myLeave;
        let tableCode = myLeave.activeTab + '_list';
        dispatch({
            type: 'myLeave/queryListAction',
            payload: {
                key: myLeave.activeTab,
                beginDate: beginValue,
                endDate: endValue
            }
        }).then(res => {
            if (res.success) {
                if (res.data) {
                    editTable.setTableData(tableCode, res.data[tableCode]);
                    dispatch({
                        type: 'myLeave/update',
                        payload: {
                            [myLeave.activeTab + 'PageInfo']: res.data[tableCode].pageInfo
                        }
                    })
                }
            }
        })
    }
    /**
     * @desc: record(行数据) ，index(当前index) props, e (事件对象)
     * @param {type}
     * @return:
     */
    onRowDoubleClick = (record, index, props, eve) => {
        const {dispatch, myLeave, form, renderItem} = this.comp.props;
        let minTime = '';
        this.onRowDoubleClick_attend(record, index, props, eve)

        dispatch({
            type: 'myLeave/update',
            payload: {
                showMode: 'card',
                isEdit: false,
                minTime,
                billId: record.values['pk_' + myLeave.activeTab].value,
                headerTitle: myLeave.subTitle,
                approvestatus: record.values['approvestatus'] && record.values['approvestatus'].value
            }
        })
    }

    onRowDoubleClick_attend = (record, index, props, eve) => {
        const {dispatch, myLeave, form, editTable, meta} = this.comp.props;
        const {json = {}} = myLeave;
        let minTime = '';
        let formCode = myLeave.activeTab + '_card';
        const {setFormStatus, setAllFormValue, setItemsVisible, EmptyAllFormValue, setFormItemsRequired} = form;
        EmptyAllFormValue(formCode);
        setFormStatus(formCode, 'browse');
        editTable.setStatus('batch_attend_edit_list', 'browse')
        let cardDefaultVisiable = {
            fill_date: false,
            original_sign_status: false,
            original_sign_time: false,
            fill_time: false
        }
        let cardDefaultRequired = {
            fill_type_id: true
        }
        setItemsVisible(formCode, cardDefaultVisiable);
        setFormItemsRequired(formCode, cardDefaultRequired);
        setAllFormValue({
            [formCode]: {
                rows: [record]
            }
        })
        let batch_attend_list = {
            areacode: 'batch_attend_edit_list',
            rows: []
        }
        let isFreeAdd = true;
        if (record.values.fill_details
            && record.values.fill_details.value) {
            let tempData = JSON.parse(record.values.fill_details.value)
            tempData.forEach((item, index) => {
                if (item['originalSignStatus']) {
                    isFreeAdd = false
                }
                let values = {
                    fillDate: {
                        value: typeof item['fillDate'] === 'number' ? formatDate(item['fillDate']): item['fillDate']
                    },
                    fillTime: {
                        value: typeof item['fillTime'] === 'number' ? formatDate(item['fillTime'], ':', 'hh-mm'): item['fillTime'].substring(0,5)
                    },
                    originalSignTime: {
                        value: item['originalSignTime'] ? formatDate(item['originalSignTime'], ' ', 'y-m-d-h-m'): ''
                    },
                    listOrder: {
                        value: item['listOrder']
                    },
                    isNeedFill: {
                        value: ''
                    },
                    signState: {
                        value: item['originalSignStatus'],
                        display: getSignStatus(item['originalSignStatus'], json)
                    },
                    originalSignStatus: {
                        value: item['originalSignState']
                    },
                    fillStatus: {
                        value: ''
                    },
                    fill_reason: {
                        value: item['fill_reason']
                    }
                };
                batch_attend_list.rows.push({
                    status: '0',
                    values
                })
            })
        }
        editTable.setTableData('batch_attend_edit_list', batch_attend_list);
        let temp = meta.getMeta();
        temp['batch_attend_edit_list'].items.forEach(item => {
            if (item.attrcode === "originalSignTime") {
                item.visible = !isFreeAdd
            }
        });
        meta.setMeta(temp);
        temp = null
        dispatch({
            type: 'myLeave/update',
            payload: {
                showMode: 'card',
                isEdit: false,
                isFreeAdd,
                minTime,
                billId: record.values['pk_' + myLeave.activeTab].value,
                headerTitle: myLeave.subTitle,
                approvestatus: record.values['approvestatus'] && record.values['approvestatus'].value
            }
        })
    }

    didMount = async () => {
        const {myLeave, dispatch} = this.comp.props;
        const {attendancePageInfo} = myLeave
        let pageSize = await getSysConf();

        dispatch({
            type: 'myLeave/update',
            payload: {
                attendancePageInfo: {
                    ...attendancePageInfo,
                    pageSize: pageSize
                }
            }
        })
        this.queryListAction()
    }
}